/*
Name:digger_plugin
Author: Terry Dong
Description: data access layer
*/
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using Gammar.Digger.SystemFramework;
using Gammar.Digger.Entity;
using Gammar.Digger.IDAL;
using Gammar.Digger.Data;
namespace Gammar.Digger.DALSqlServer
{
    public partial class digger_plugin : Idigger_plugin
    {
        private DbSqlServer helper;

        #region constructors
        /// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public digger_plugin()
        {
            helper = new DbSqlServer(ConnectionString.SqlServer_GPriceCollect);
        }

        /// <summary>
        /// default constructor with the defaultConn
        /// </summary>
        public digger_plugin(string connString)
        {
            helper = new DbSqlServer(connString);
        }
        #endregion

        #region IDataBase<digger_plugin>

        public int Add(digger_pluginEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO digger_plugin(");
            strSql.Append("name,start_url,is_download_image,is_download_flash,max_number,list_area_rule,list_url_rule,list_nextPage_rule,content_url_rule,content_nextPage_rule,content_title_rule,content_detail_rule,content_originalPrice_rule,content_salePrice_rule,content_CreateDate_rule,is_valid,create_time,last_modify_time,dig_depth,run_status,thread_number,site_id,category_id,page_Encode)");
            strSql.Append(" VALUES (");
            strSql.Append("@name,@start_url,@is_download_image,@is_download_flash,@max_number,@list_area_rule,@list_url_rule,@list_nextPage_rule,@content_url_rule,@content_nextPage_rule,@content_title_rule,@content_detail_rule,@content_originalPrice_rule,@content_salePrice_rule,@content_CreateDate_rule,@is_valid,@create_time,@last_modify_time,@dig_depth,@run_status,@thread_number,@site_id,@category_id,@page_Encode)");
            SqlParameter[] parameters = {
					new SqlParameter("@name",SqlDbType.VarChar),
					new SqlParameter("@start_url",SqlDbType.VarChar),
					new SqlParameter("@is_download_image",SqlDbType.Bit),
					new SqlParameter("@is_download_flash",SqlDbType.Bit),
					new SqlParameter("@max_number",SqlDbType.Int),
					new SqlParameter("@list_area_rule",SqlDbType.VarChar),
					new SqlParameter("@list_url_rule",SqlDbType.VarChar),
					new SqlParameter("@list_nextPage_rule",SqlDbType.VarChar),
					new SqlParameter("@content_url_rule",SqlDbType.VarChar),
					new SqlParameter("@content_nextPage_rule",SqlDbType.VarChar),
					new SqlParameter("@content_title_rule",SqlDbType.VarChar),
					new SqlParameter("@content_detail_rule",SqlDbType.VarChar),
					new SqlParameter("@content_originalPrice_rule",SqlDbType.VarChar),
					new SqlParameter("@content_salePrice_rule",SqlDbType.VarChar),
					new SqlParameter("@content_CreateDate_rule",SqlDbType.VarChar),
					new SqlParameter("@is_valid",SqlDbType.Bit),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@last_modify_time",SqlDbType.BigInt),
					new SqlParameter("@dig_depth",SqlDbType.Int),
					new SqlParameter("@run_status",SqlDbType.Int),
					new SqlParameter("@thread_number",SqlDbType.Int),
					new SqlParameter("@site_id",SqlDbType.Int),
					new SqlParameter("@category_id",SqlDbType.UniqueIdentifier),
					new SqlParameter("@page_Encode",SqlDbType.VarChar)
					};
            parameters[0].Value = entity.name;
            parameters[1].Value = entity.start_url;
            parameters[2].Value = entity.is_download_image;
            parameters[3].Value = entity.is_download_flash;
            parameters[4].Value = entity.max_number;
            parameters[5].Value = entity.list_area_rule;
            parameters[6].Value = entity.list_url_rule;
            parameters[7].Value = entity.list_nextPage_rule;
            parameters[8].Value = entity.content_url_rule;
            parameters[9].Value = entity.content_nextPage_rule;
            parameters[10].Value = entity.content_title_rule;
            parameters[11].Value = entity.content_detail_rule;
            parameters[12].Value = entity.content_originalPrice_rule;
            parameters[13].Value = entity.content_salePrice_rule;
            parameters[14].Value = entity.content_CreateDate_rule;
            parameters[15].Value = entity.is_valid;
            parameters[16].Value = entity.create_time;
            parameters[17].Value = entity.last_modify_time;
            parameters[18].Value = entity.dig_depth;
            parameters[19].Value = entity.run_status;
            parameters[20].Value = entity.thread_number;
            parameters[21].Value = entity.site_id;
            parameters[22].Value = entity.category_id;
            parameters[23].Value = entity.page_Encode;

            return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        public int Update(digger_pluginEntity entity)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE digger_plugin SET ");
            strSql.Append("name=@name,");
            strSql.Append("start_url=@start_url,");
            strSql.Append("is_download_image=@is_download_image,");
            strSql.Append("is_download_flash=@is_download_flash,");
            strSql.Append("max_number=@max_number,");
            strSql.Append("list_area_rule=@list_area_rule,");
            strSql.Append("list_url_rule=@list_url_rule,");
            strSql.Append("list_nextPage_rule=@list_nextPage_rule,");
            strSql.Append("content_url_rule=@content_url_rule,");
            strSql.Append("content_nextPage_rule=@content_nextPage_rule,");
            strSql.Append("content_title_rule=@content_title_rule,");
            strSql.Append("content_detail_rule=@content_detail_rule,");
            strSql.Append("content_originalPrice_rule=@content_originalPrice_rule,");
            strSql.Append("content_salePrice_rule=@content_salePrice_rule,");
            strSql.Append("content_CreateDate_rule=@content_CreateDate_rule,");
            strSql.Append("is_valid=@is_valid,");
            strSql.Append("create_time=@create_time,");
            strSql.Append("last_modify_time=@last_modify_time,");
            strSql.Append("dig_depth=@dig_depth,");
            strSql.Append("run_status=@run_status,");
            strSql.Append("thread_number=@thread_number,");
            strSql.Append("site_id=@site_id,");
            strSql.Append("category_id=@category_id,");
            strSql.Append("page_Encode=@page_Encode");

            strSql.Append(" WHERE plugin_id=@plugin_id");
            SqlParameter[] parameters = {
					new SqlParameter("@plugin_id",SqlDbType.Int),
					new SqlParameter("@name",SqlDbType.VarChar),
					new SqlParameter("@start_url",SqlDbType.VarChar),
					new SqlParameter("@is_download_image",SqlDbType.Bit),
					new SqlParameter("@is_download_flash",SqlDbType.Bit),
					new SqlParameter("@max_number",SqlDbType.Int),
					new SqlParameter("@list_area_rule",SqlDbType.VarChar),
					new SqlParameter("@list_url_rule",SqlDbType.VarChar),
					new SqlParameter("@list_nextPage_rule",SqlDbType.VarChar),
					new SqlParameter("@content_url_rule",SqlDbType.VarChar),
					new SqlParameter("@content_nextPage_rule",SqlDbType.VarChar),
					new SqlParameter("@content_title_rule",SqlDbType.VarChar),
					new SqlParameter("@content_detail_rule",SqlDbType.VarChar),
					new SqlParameter("@content_originalPrice_rule",SqlDbType.VarChar),
					new SqlParameter("@content_salePrice_rule",SqlDbType.VarChar),
					new SqlParameter("@content_CreateDate_rule",SqlDbType.VarChar),
					new SqlParameter("@is_valid",SqlDbType.Bit),
					new SqlParameter("@create_time",SqlDbType.BigInt),
					new SqlParameter("@last_modify_time",SqlDbType.BigInt),
					new SqlParameter("@dig_depth",SqlDbType.Int),
					new SqlParameter("@run_status",SqlDbType.Int),
					new SqlParameter("@thread_number",SqlDbType.Int),
					new SqlParameter("@site_id",SqlDbType.Int),
					new SqlParameter("@category_id",SqlDbType.UniqueIdentifier),
					new SqlParameter("@page_Encode",SqlDbType.VarChar)
					};
            parameters[0].Value = entity.plugin_id;
            parameters[1].Value = entity.name;
            parameters[2].Value = entity.start_url;
            parameters[3].Value = entity.is_download_image;
            parameters[4].Value = entity.is_download_flash;
            parameters[5].Value = entity.max_number;
            parameters[6].Value = entity.list_area_rule;
            parameters[7].Value = entity.list_url_rule;
            parameters[8].Value = entity.list_nextPage_rule;
            parameters[9].Value = entity.content_url_rule;
            parameters[10].Value = entity.content_nextPage_rule;
            parameters[11].Value = entity.content_title_rule;
            parameters[12].Value = entity.content_detail_rule;
            parameters[13].Value = entity.content_originalPrice_rule;
            parameters[14].Value = entity.content_salePrice_rule;
            parameters[15].Value = entity.content_CreateDate_rule;
            parameters[16].Value = entity.is_valid;
            parameters[17].Value = entity.create_time;
            parameters[18].Value = entity.last_modify_time;
            parameters[19].Value = entity.dig_depth;
            parameters[20].Value = entity.run_status;
            parameters[21].Value = entity.thread_number;
            parameters[22].Value = entity.site_id;
            parameters[23].Value = entity.category_id;
            parameters[24].Value = entity.page_Encode;

            return helper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        public int UpdateByplugin_id(digger_pluginEntity entity)
        {
            string where = string.Format("plugin_id='{0}'", entity.plugin_id);
            entity.plugin_id = null;
            return Updatepartial(entity, where);
        }

        public int Delete(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM digger_plugin ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return helper.ExecuteNonQuery(strSql.ToString());
        }

        public digger_pluginEntity GetEntity(int primaryKeyId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from digger_plugin ");
            strSql.Append(" where plugin_id=@primaryKeyId");
            SqlParameter[] parameters = {
					new SqlParameter("@primaryKeyId", SqlDbType.Int)};
            parameters[0].Value = primaryKeyId;
            SqlDataReader dr = helper.ExecuteReader(strSql.ToString(), parameters) as SqlDataReader;
            if (dr.HasRows)
            {
                dr.Read();
                digger_pluginEntity entity = new digger_pluginEntity();
                if (!Convert.IsDBNull(dr["plugin_id"]))
                {
                    entity.plugin_id = (int)dr["plugin_id"];
                }
                entity.name = dr["name"].ToString();
                entity.start_url = dr["start_url"].ToString();
                if (!Convert.IsDBNull(dr["is_download_image"]))
                {
                    entity.is_download_image = (bool)dr["is_download_image"];
                }
                if (!Convert.IsDBNull(dr["is_download_flash"]))
                {
                    entity.is_download_flash = (bool)dr["is_download_flash"];
                }
                if (!Convert.IsDBNull(dr["max_number"]))
                {
                    entity.max_number = (int)dr["max_number"];
                }
                entity.list_area_rule = dr["list_area_rule"].ToString();
                entity.list_url_rule = dr["list_url_rule"].ToString();
                entity.list_nextPage_rule = dr["list_nextPage_rule"].ToString();
                entity.content_url_rule = dr["content_url_rule"].ToString();
                entity.content_nextPage_rule = dr["content_nextPage_rule"].ToString();
                entity.content_title_rule = dr["content_title_rule"].ToString();
                entity.content_detail_rule = dr["content_detail_rule"].ToString();
                entity.content_originalPrice_rule = dr["content_originalPrice_rule"].ToString();
                entity.content_salePrice_rule = dr["content_salePrice_rule"].ToString();
                entity.content_CreateDate_rule = dr["content_CreateDate_rule"].ToString();
                if (!Convert.IsDBNull(dr["is_valid"]))
                {
                    entity.is_valid = (bool)dr["is_valid"];
                }
                if (!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
                if (!Convert.IsDBNull(dr["last_modify_time"]))
                {
                    entity.last_modify_time = (long)dr["last_modify_time"];
                }
                if (!Convert.IsDBNull(dr["dig_depth"]))
                {
                    entity.dig_depth = (int)dr["dig_depth"];
                }
                if (!Convert.IsDBNull(dr["run_status"]))
                {
                    entity.run_status = (int)dr["run_status"];
                }
                if (!Convert.IsDBNull(dr["thread_number"]))
                {
                    entity.thread_number = (int)dr["thread_number"];
                }
                if (!Convert.IsDBNull(dr["site_id"]))
                {
                    entity.site_id = (int)dr["site_id"];
                }
                if (!Convert.IsDBNull(dr["category_id"]))
                {
                    entity.category_id = (Guid)dr["category_id"];
                }
                entity.page_Encode = dr["page_Encode"].ToString();
                dr.Close();
                dr.Dispose();
                return entity;
            }
            else
            {
                dr.Close();
                dr.Dispose();
                return null;
            }
        }

        public object GetCell(string cellName, string where)
        {
            string strSql = string.Empty;
            if (string.IsNullOrEmpty(where))
            {
                strSql = string.Format("SELECT {0} FROM digger_plugin", cellName);
            }
            else
            {
                strSql = string.Format("SELECT {0} FROM digger_plugin Where {1}", cellName, where);
            }
            return helper.ExecuteCell(strSql);
        }

        public DataTable GetTable(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT plugin_id,name,start_url,is_download_image,is_download_flash,max_number,list_area_rule,list_url_rule,list_nextPage_rule,content_url_rule,content_nextPage_rule,content_title_rule,content_detail_rule,content_originalPrice_rule,content_salePrice_rule,content_CreateDate_rule,is_valid,create_time,last_modify_time,dig_depth,run_status,thread_number,site_id,category_id,page_Encode");
            strSql.Append(" FROM digger_plugin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            return helper.ExecuteAdapter(strSql.ToString());
        }

        public SortList<digger_pluginEntity> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select plugin_id,name,start_url,is_download_image,is_download_flash,max_number,list_area_rule,list_url_rule,list_nextPage_rule,content_url_rule,content_nextPage_rule,content_title_rule,content_detail_rule,content_originalPrice_rule,content_salePrice_rule,content_CreateDate_rule,is_valid,create_time,last_modify_time,dig_depth,run_status,thread_number,site_id,category_id,page_Encode");
            strSql.Append(" FROM digger_plugin ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            SqlDataReader dr = helper.ExecuteReader(strSql.ToString()) as SqlDataReader;
            SortList<digger_pluginEntity> list = new SortList<digger_pluginEntity>();
            while (dr.Read())
            {
                digger_pluginEntity entity = new digger_pluginEntity();
                if (!Convert.IsDBNull(dr["plugin_id"]))
                {
                    entity.plugin_id = (int)dr["plugin_id"];
                }
                entity.name = dr["name"].ToString();
                entity.start_url = dr["start_url"].ToString();
                if (!Convert.IsDBNull(dr["is_download_image"]))
                {
                    entity.is_download_image = (bool)dr["is_download_image"];
                }
                if (!Convert.IsDBNull(dr["is_download_flash"]))
                {
                    entity.is_download_flash = (bool)dr["is_download_flash"];
                }
                if (!Convert.IsDBNull(dr["max_number"]))
                {
                    entity.max_number = (int)dr["max_number"];
                }
                entity.list_area_rule = dr["list_area_rule"].ToString();
                entity.list_url_rule = dr["list_url_rule"].ToString();
                entity.list_nextPage_rule = dr["list_nextPage_rule"].ToString();
                entity.content_url_rule = dr["content_url_rule"].ToString();
                entity.content_nextPage_rule = dr["content_nextPage_rule"].ToString();
                entity.content_title_rule = dr["content_title_rule"].ToString();
                entity.content_detail_rule = dr["content_detail_rule"].ToString();
                entity.content_originalPrice_rule = dr["content_originalPrice_rule"].ToString();
                entity.content_salePrice_rule = dr["content_salePrice_rule"].ToString();
                entity.content_CreateDate_rule = dr["content_CreateDate_rule"].ToString();
                if (!Convert.IsDBNull(dr["is_valid"]))
                {
                    entity.is_valid = (bool)dr["is_valid"];
                }
                if (!Convert.IsDBNull(dr["create_time"]))
                {
                    entity.create_time = (long)dr["create_time"];
                }
                if (!Convert.IsDBNull(dr["last_modify_time"]))
                {
                    entity.last_modify_time = (long)dr["last_modify_time"];
                }
                if (!Convert.IsDBNull(dr["dig_depth"]))
                {
                    entity.dig_depth = (int)dr["dig_depth"];
                }
                if (!Convert.IsDBNull(dr["run_status"]))
                {
                    entity.run_status = (int)dr["run_status"];
                }
                if (!Convert.IsDBNull(dr["thread_number"]))
                {
                    entity.thread_number = (int)dr["thread_number"];
                }
                if (!Convert.IsDBNull(dr["site_id"]))
                {
                    entity.site_id = (int)dr["site_id"];
                }
                if (!Convert.IsDBNull(dr["category_id"]))
                {
                    entity.category_id = (Guid)dr["category_id"];
                }
                entity.page_Encode = dr["page_Encode"].ToString();
                list.Add(entity);
            }

            dr.Close();
            dr.Dispose();

            return list;
        }


        #region select,add,update the fields that you want to operate.

        public int AddPartial(digger_pluginEntity entity)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildAdd(entity, "digger_plugin", ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql, param);
            }
            return reval;

        }

        public int Updatepartial(digger_pluginEntity entity, string where)
        {
            int reval = 0;
            string sql = string.Empty;
            SqlParameter[] param = DataUtility.BuildUpdate(entity, "digger_plugin", where, ref sql);

            if (param != null)
            {
                reval = helper.ExecuteNonQuery(sql, param);
            }
            return reval;
        }

        public List<digger_pluginEntity> GetPartial(string[] selectedFields, string where)
        {
            List<digger_pluginEntity> list = new List<digger_pluginEntity>();
            string sql = DataUtility.BuildSelect(selectedFields, "digger_plugin", where);
            if (!string.IsNullOrEmpty(sql))
            {
                DbDataReader reader = helper.ExecuteReader(sql);

                while (reader.Read())
                {
                    digger_pluginEntity model = new digger_pluginEntity();
                    foreach (string field in selectedFields)
                    {
                        PropertyInfo propInfo = model.GetType().GetProperty(field);
                        propInfo.SetValue(model, reader[field], null);
                    }

                    list.Add(model);
                }

                reader.Close();
            }
            return list;
        }
        #endregion


        #endregion
    }
}
